import { _decorator, Component, Node, Sprite, resources, SpriteFrame, Label, director } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends Component {
    @property(Node)
    public rival: Node = null;

    @property(Node)
    public rps: Node = null;

    @property(Label)
    public label: Label = null;

    private currentFrameIndex: number = 0;
    private framesRival: SpriteFrame[] = [];
    private framesRps: SpriteFrame[] = [];
    private flag: number = 0;

    start() {
        this.loadSpriteFramesRival();
        this.loadSpriteFramesRps();
    }


    private async loadSpriteFramesRps() {
        // // 动态加载 SpriteFrame，注意路径结尾没有文件扩展名
        // resources.load('sprites/paper/spriteFrame', SpriteFrame, (err, spriteFrame) => {
        //     if (err) {
        //         console.error(err);
        //         return;
        //     }
        //     this.framesRps.push(spriteFrame);
        //     this.checkFramesRpsLoaded();
        // });

        // resources.load('sprites/rock/spriteFrame', SpriteFrame, (err, spriteFrame) => {
        //     if (err) {
        //         console.error(err);
        //         return;
        //     }
        //     this.framesRps.push(spriteFrame);
        //     this.checkFramesRpsLoaded();
        // });

        // resources.load('sprites/scissors/spriteFrame', SpriteFrame, (err, spriteFrame) => {
        //     if (err) {
        //         console.error(err);
        //         return;
        //     }
        //     this.framesRps.push(spriteFrame);
        //     this.checkFramesRpsLoaded();
        // });

        await this.loadRpsAsync('sprites/paper/spriteFrame');
        await this.loadRpsAsync('sprites/rock/spriteFrame');
        await this.loadRpsAsync('sprites/scissors/spriteFrame');
        this.schedule(this.changeSpriteFrame, 0.1); // 每秒切换一次
    }


    public loadRpsAsync(path:string): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            resources.load(path, SpriteFrame, (err: Error, spriteFrame) => {
                if (!err) {
                    this.framesRps.push(spriteFrame);
                    resolve();
                } else {
                    console.error(err);
                    reject();
                }
            });
        });
    }



    private async loadSpriteFramesRival() {
        // 动态加载 SpriteFrame，注意路径结尾没有文件扩展名
        await this.loadRivalAsync('sprites/avatar-before/spriteFrame');
        await this.loadRivalAsync('sprites/avatar-after/spriteFrame');
    }

    public loadRivalAsync(path:string): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            resources.load(path, SpriteFrame, (err: Error, spriteFrame) => {
                if (!err) {
                    this.framesRival.push(spriteFrame);
                    resolve();
                } else {
                    console.error(err);
                    reject();
                }
            });
        });
    }

    private changeSpriteFrame() {
        // 确保framesRps数组中有多个元素，否则无法切换
        if (this.framesRps.length > 1) {
            let nextFrameIndex;
            do {
                // 生成一个随机索引
                nextFrameIndex = Math.floor(Math.random() * this.framesRps.length);
            } while (nextFrameIndex === this.currentFrameIndex); // 确保不等于当前索引
    
            // 更新当前帧索引
            this.currentFrameIndex = nextFrameIndex;
    
            // 将rps的Sprite组件的SpriteFrame设置为新选择的SpriteFrame
            this.rps.getComponent(Sprite).spriteFrame = this.framesRps[this.currentFrameIndex];
        }
    }


    attack(event, customEventData) {
        if(this.flag != 0){
            return;
        }
        this.flag = 1;
        this.rival.getComponent(Sprite).spriteFrame = this.framesRival[1];
        let type = event.target.name;
        let ack = this.currentFrameIndex;
        console.log(type);
        this.unschedule(this.changeSpriteFrame);
        if (type == 'paper') {
            if(ack == 0){
                this.label.string = '平局';
            }else if(ack == 1) {
                this.label.string = '你赢了';
            }else{
                this.label.string = '你输了';
            }
        } else if (type == 'rock') {
            if(ack == 0){
                this.label.string = '你输了';
            }else if(ack == 1) {
                this.label.string = '平局';
            }else{
                this.label.string = '你赢了';
            }
        } else {
            if(ack == 0){
                this.label.string = '你赢了';
            }else if(ack == 1) {
                this.label.string = '你输了';
            }else{
                this.label.string = '平局';
            }
        }
    }
    update(deltaTime: number) {
    }

    restart(){
        director.loadScene('Game');
    }
}